package com.apes.scm.account.account.model;

import com.apes.framework.jpa.ApesBean;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.util.JpaUtil;
import com.apes.framework.util.LogicalUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Objects;

/**
 * 功能：合作方式
 *
 * @author xul
 * @create 2018-02-11 14:16
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = "acc_cooperation", indexes = {
        @Index(columnList = "inventory_mode,accounting_method", unique = true)})
public class Cooperation extends Model {

    @Id
    @Column(name = "id", length = 30)
    private String id;  // distribution 经销;  consignment  代销; consortium  联营

    @Column(name = "name", nullable = false, length = 60)
    private String name; //名称

    /**
     * 库存方式
     * 1. 适用于：经销
     *    IM_OWN: 自有
     * 2. 适用于：代销、联营
     *    IM_CONSIGN: 寄售
     */
    @Name
    @Column(name = "inventory_mode", nullable = false, length = 20)
    private String inventoryMode; //库存方式

    public void setInventoryMode(String inventoryMode) {
        if (LogicalUtil.notIn(inventoryMode, InventoryMode.IM_OWN, InventoryMode.IM_CONSIGN)) {
            throw new RuntimeException("库存方式输入错误！");
        }
        this.inventoryMode = inventoryMode;
    }

    /**
     * 采购核算方式
     * 1. 适用于：经销、代销
     *    AM_PURCHASE: 按采购价核算
     * 2. 适用于：联营
     *    AM_SALE: 按售价（或码价）核算
     *
     */
    @Name
    @Column(name = "accounting_method", nullable = false, length = 20)
    private String accountingMethod;

    public void setAccountingMethod(String accountingMethod) {
        if (LogicalUtil.notIn(accountingMethod, AccountingMethod.AM_PURCHASE, AccountingMethod.AM_SALE, AccountingMethod.AM_REAL)) {
            throw new RuntimeException("核算方式输入错误！");
        }
        this.accountingMethod = accountingMethod;
    }

    @Column(name = "priority", length = 3, nullable = false)
    private String priority; //优先级（成本核算，越小优先级越高）

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != JpaUtil.getDomainClass((ApesBean) o)) return false;
        Cooperation cooperation = (Cooperation) o;
        return Objects.equals(id, cooperation.getId());
    }
}
